package cn.iune.erp.inventory;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.justep.baas.Utils;
import com.justep.baas.data.Row;
import com.justep.baas.data.RowState;
import com.justep.baas.data.Table;
import com.justep.baas.data.Transform;



@Transactional
@Service
public class StorageLedgerDetailServiceImpl implements IStorageLedgerDetailService {
	private final Logger log = LogManager.getLogger(this);

	@Autowired
	private IStorageLedgerDetailDao dao;
	
	
	
	
	public StorageLedgerDetailServiceImpl(){
//		dao = new StorageItemBookDaoJPAImpl();
	}
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStorageItemBookService#getAll()
	 */
	@Override
	public List<StorageLedgerDetail> getAll(){
		return dao.getAll();
	}
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStorageItemBookService#getByID(java.lang.String)
	 */
	@Override
	public StorageLedgerDetail getByID(String fID) {
		return dao.getByID(fID);
	}
	
	@Transactional
	@Override
	public void insert(StorageLedgerDetail obj) {
		dao.insert(obj);
	}

	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStorageItemBookService#delete(java.lang.String)
	 */
	@Override
	@Transactional
	public void delete(String fID) {
		dao.delete(fID);
	}
	
	@Transactional
	@Override
	public StorageLedgerDetail update(StorageLedgerDetail obj) {
		return dao.update(obj);
	}

	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStorageItemBookService#getByNo(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public StorageLedgerDetail getByNo(String recordNo, String serialNo, String revOpay, String enterpriseNo) {
		return dao.getByNo(recordNo, serialNo, revOpay, enterpriseNo);
	}
	
	@Transactional
	@Override
	public JSONObject save(JSONObject object) throws ParseException {
//		System.out.println("StorageItemBookServiceImpl。save");
//		System.out.println(object.toJSONString());
		// TODO Auto-generated method stub
		// JSON转换Table
		Table table = Transform.jsonToTable(object);
		
		JSONObject json = new JSONObject();
		json.put("concept", "StorageItemBook");
		JSONArray rows = new JSONArray();

		List<Row> list = table.getRows();
		for(Row row : list){
			JSONObject javaObject= Transform.rowToJavaObject(table, row);
//			System.out.println(javaObject.toJSONString());
			StorageLedgerDetail obj = JSON.toJavaObject(javaObject, StorageLedgerDetail.class);
//			System.out.println("RowState-->"+obj.getRowState());
			if(RowState.NEW.equals(obj.getRowState())){
				dao.insert(obj);
			}
			if(RowState.DELETE.equals(obj.getRowState())){
				dao.delete(obj.getfID());
			}
			if(RowState.EDIT.equals(obj.getRowState())){
				StorageLedgerDetail objNew = dao.update(obj);
				JSONObject jsonRow = new JSONObject();
				jsonRow.put("id", obj.getfID());
				jsonRow.put("version", objNew.getVersion());
				rows.add(jsonRow);
			}
		}
		
		json.put("rows", rows);
		
		return json;
		
	}
	
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStorageItemBookService#getByMasterID(java.lang.String)
	 */
	@Override
	public List<StorageLedgerDetail> getByMasterID(String fMasterID) {
		return dao.getByMasterID(fMasterID);
	}
/*
	public JSONObject query(JSONObject data) throws Throwable {
		
		// 获取参数
		Object columns = data.getJSONObject("columns"); // 列定义
		String orderBy = data.getString("orderBy"); // 排序
		Integer limit = data.getInteger("limit"); // 分页查询的行数
		Integer offset = data.getInteger("offset"); // 分页查询的行偏移
		String search = data.getString("search"); // 检索关键字
		String filter = data.getString("filter"); // 过滤条件
		String id = data.getString("id");
		String fMasterID = data.getString("fMasterID");

		
		// 存放SQL中的参数值
		List<Object> sqlParams = new ArrayList<Object>();
		// 存放SQL中的过滤条件
		List<String> filters = new ArrayList<String>();
		if (!CommonService.isEmptyString(search)) {
			// 增加过滤条件
			filters.add("recordNo LIKE ? OR originNo LIKE ? OR comments LIKE ?");
			// 检索关键字中如果没有%，则前后自动加%
			search = (search.indexOf("%") != -1) ? search : "%" + search + "%";
			// sqlParams的参数个数和顺序必须与过滤条件的?相匹配 
			for (int i = 0; i < 3; i++) {
				sqlParams.add(search);
			}
		}
		//处理过滤条件
		if (!CommonService.isEmptyString(filter)) {
			filters.add(filter);
		}
		//处理id
		if (!CommonService.isEmptyString(id)) {
			filters.add("fID='"+id+"'");
		}
		//处理fMasterID
//		System.out.println("fMasterID-->"+fMasterID);
		if (!CommonService.isEmptyString(fMasterID)) {
			filters.add("fMasterID='"+fMasterID+"'");
		}
	
		
		Table table = Transform.createTableByColumnsDefine((JSONObject) columns);
		
		if (offset != null && offset.equals(0)) {
			long rowCount = dao.getCount(filters, sqlParams);
			table.setTotal((int)rowCount);
		}

//		String orderBy = "serialNo DESC";
				
		//获取数据
		List<Row> rows = listToRows(dao.query(filters,orderBy,sqlParams,offset,limit));
	    table.appendRows(rows);
		JSONObject json = Transform.tableToJson(table);
	
		return json;
	}

	public int save(JSONObject object) throws Throwable {
		// TODO Auto-generated method stub
//		log.info("@RequestParam------"+object.toJSONString());
		// JSON转换Table
		Table table = Transform.jsonToTable(object);
		List<StorageItemBook> list = rowsToList(table.getRows());

		for(StorageItemBook obj : list){
			if(RowState.NEW.equals(obj.getRowState())){
				dao.insert(obj);
			}
			if(RowState.EDIT.equals(obj.getRowState())){
				dao.update(obj);
			}
			if(RowState.DELETE.equals(obj.getRowState())){
				dao.delete(obj.getfID());
			}
		}
		return 0;
	}

	public List<Row> listToRows(List<StorageItemBook> list){
		//添加行集
		List<Row> rows = new ArrayList<Row>();
		//循环行
	    for(StorageItemBook o : list){
	    	rows.add(objectToRow(o));
	    }
	    return rows;
	}

	public List<StorageItemBook> rowsToList(List<Row> rows){
		//添加行集
		List<StorageItemBook> list = new ArrayList<StorageItemBook>();
		//循环行
	    for(Row r : rows){
	    	list.add(rowToObject(r));
	    }
	    return list;
	}

	public Row objectToRow(StorageItemBook obj){
    	
		Map<String, ColumnValue> values = new HashMap<String, ColumnValue>();
		//循环行中每一列
		ColumnValue value = new ColumnValue(obj.getfID());
		values.put("fID", value);
		value = new ColumnValue(obj.getSystemCode());
 		values.put("systemCode", value);
		value = new ColumnValue(obj.getBillType());
 		values.put("billType", value);
		value = new ColumnValue(obj.getBillTypeName());
 		values.put("billTypeName", value);
		value = new ColumnValue(obj.getOrderDate());
 		values.put("orderDate", value);
		value = new ColumnValue(obj.getState());
 		values.put("state", value);
		value = new ColumnValue(obj.getStatus());
 		values.put("status", value);
 		
		value = new ColumnValue(obj.getRevOpay());
 		values.put("revOpay", value);
		value = new ColumnValue(obj.getDirection());
 		values.put("direction", value);
		value = new ColumnValue(obj.getCostingCode());
 		values.put("costingCode", value);
		value = new ColumnValue(obj.getAudit());
 		values.put("audit", value);
		value = new ColumnValue(obj.getConfirmDate());
 		values.put("confirmDate", value);

		value = new ColumnValue(obj.getOrigin());
 		values.put("origin", value);
		value = new ColumnValue(obj.getOriginName());
 		values.put("originName", value);
		value = new ColumnValue(obj.getOriginNo());
 		values.put("originNo", value);
 		
		value = new ColumnValue(obj.getRecordNo());
 		values.put("recordNo", value);

		value = new ColumnValue(obj.getFactoryNo());
 		values.put("factoryNo", value);
		value = new ColumnValue(obj.getFactoryName());
 		values.put("factoryName", value);
 		
		value = new ColumnValue(obj.getStorageTypeD());
 		values.put("storageTypeD", value);
		value = new ColumnValue(obj.getStorageTypeDLabel());
 		values.put("storageTypeDLabel", value);
		value = new ColumnValue(obj.getProviderNo());
 		values.put("providerNo", value);
		value = new ColumnValue(obj.getShortName());
 		values.put("shortName", value);
		value = new ColumnValue(obj.getProviderName());
 		values.put("providerName", value);
 		
		value = new ColumnValue(obj.getStorageType());
 		values.put("storageType", value);
		value = new ColumnValue(obj.getStorageTypeLabel());
 		values.put("storageTypeLabel", value);
		value = new ColumnValue(obj.getStorageNo());
 		values.put("storageNo", value);
		value = new ColumnValue(obj.getStorageName());
 		values.put("storageName", value);
		value = new ColumnValue(obj.getFloorNo());
 		values.put("floorNo", value);
 		
		value = new ColumnValue(obj.getTaxType());
 		values.put("taxType", value);
		value = new ColumnValue(obj.getTaxTypeLabel());
 		values.put("taxTypeLabel", value);
		value = new ColumnValue(obj.getTaxRate());
 		values.put("taxRate", value);
		
		value = new ColumnValue(obj.getfMasterID());
 		values.put("fMasterID", value);
		
		value = new ColumnValue(obj.getItemNo());
 		values.put("itemNo", value);
		value = new ColumnValue(obj.getItemDescribe());
 		values.put("itemDescribe", value);
		value = new ColumnValue(obj.getItemProperty());
 		values.put("itemProperty", value);
		value = new ColumnValue(obj.getUnitCode());
 		values.put("unitCode", value);
		value = new ColumnValue(obj.getUnitName());
 		values.put("unitName", value);
		value = new ColumnValue(obj.getProcess());
 		values.put("process", value);
		value = new ColumnValue(obj.getProcessLabel());
 		values.put("processLabel", value);
		
		value = new ColumnValue(obj.getMaterialType());
 		values.put("materialType", value);
		value = new ColumnValue(obj.getMaterialTypeLabel());
 		values.put("materialTypeLabel", value);
		
		value = new ColumnValue(obj.getQualityStatus());
 		values.put("qualityStatus", value);
		value = new ColumnValue(obj.getQualityStatusLabel());
 		values.put("qualityStatusLabel", value);
		
		value = new ColumnValue(obj.getItemPrice());
 		values.put("itemPrice", value);
		value = new ColumnValue(obj.getDiscountRate());
 		values.put("discountRate", value);
		
		value = new ColumnValue(obj.getArriveAmount());
 		values.put("arriveAmount", value);
		value = new ColumnValue(obj.getWasterAmount());
 		values.put("wasterAmount", value);
		value = new ColumnValue(obj.getUllageAmount());
 		values.put("ullageAmount", value);
		value = new ColumnValue(obj.getArriveAmountBack());
 		values.put("arriveAmountBack", value);
		value = new ColumnValue(obj.getPriceAmount());
 		values.put("priceAmount", value);
 		
		value = new ColumnValue(obj.getItemAmount());
 		values.put("itemAmount", value);
		value = new ColumnValue(obj.getOrderAmount());
 		values.put("orderAmount", value);
		value = new ColumnValue(obj.getOrderAmountOld());
 		values.put("orderAmountOld", value);
		value = new ColumnValue(obj.getOrderAmountTax());
 		values.put("orderAmountTax", value);
		value = new ColumnValue(obj.getOrderAmountTotal());
 		values.put("orderAmountTotal", value);
 		
		value = new ColumnValue(obj.getAccountQuantityI());
 		values.put("accountQuantityI", value);
		value = new ColumnValue(obj.getAccountQuantityO());
 		values.put("accountQuantityO", value);
 		
		value = new ColumnValue(obj.getPieceQuantity());
 		values.put("pieceQuantity", value);
		value = new ColumnValue(obj.getPieceQuantityI());
 		values.put("pieceQuantityI", value);
		value = new ColumnValue(obj.getPieceQuantityO());
 		values.put("pieceQuantityO", value);
		value = new ColumnValue(obj.getQuantityL());
 		values.put("quantityL", value);
		value = new ColumnValue(obj.getCostingL());
 		values.put("costingL", value);
		value = new ColumnValue(obj.getAmountL());
 		values.put("amountL", value);
		value = new ColumnValue(obj.getPieceQuantityL());
 		values.put("pieceQuantityL", value);

		value = new ColumnValue(obj.getWasterItemIng());
 		values.put("wasterItemIng", value);
		value = new ColumnValue(obj.getWasterItemAmount());
 		values.put("wasterItemAmount", value);
		value = new ColumnValue(obj.getWasterPiece());
 		values.put("wasterPiece", value);
 		
		value = new ColumnValue(obj.getInvoiceQuantityIng());
 		values.put("invoiceQuantityIng", value);
 		value = new ColumnValue(obj.getInvoiceQuantity());
 		values.put("invoiceQuantity", value);
		value = new ColumnValue(obj.getInvoiceAmount());
 		values.put("invoiceAmount", value);
		
		value = new ColumnValue(obj.getEvaluateQuantityIng());
 		values.put("evaluateQuantityIng", value);
		value = new ColumnValue(obj.getEvaluateQuantity());
 		values.put("evaluateQuantity", value);
		value = new ColumnValue(obj.getEvaluateAmount());
 		values.put("evaluateAmount", value);
 		
		value = new ColumnValue(obj.getBackItemIng());
 		values.put("backItemIng", value);
		value = new ColumnValue(obj.getBackItemAmount());
 		values.put("backItemAmount", value);
		value = new ColumnValue(obj.getBackPiece());
 		values.put("backPiece", value);
		
		value = new ColumnValue(obj.getMaterialAmount());
 		values.put("materialAmount", value);
		value = new ColumnValue(obj.getHumanAmount());
 		values.put("workingAmount", value);
		value = new ColumnValue(obj.getManufactAmount());
 		values.put("manufactAmount", value);
		value = new ColumnValue(obj.getProcessAmount());
 		values.put("processAmount", value);
		value = new ColumnValue(obj.getCostingAmount());
 		values.put("costingAmount", value);
 		
		value = new ColumnValue(obj.getMaterialAmountPer());
 		values.put("materialAmountPer", value);
		value = new ColumnValue(obj.getHumanAmountPer());
 		values.put("workingAmountPer", value);
		value = new ColumnValue(obj.getManufactAmountPer());
 		values.put("manufactAmountPer", value);
		value = new ColumnValue(obj.getProcessAmountPer());
 		values.put("processAmountPer", value);
		value = new ColumnValue(obj.getCostingAmountPer());
 		values.put("costingAmountPer", value);

		value = new ColumnValue(obj.getDevolveCause());
 		values.put("devolveCause", value);
		value = new ColumnValue(obj.getDevolveCauseLabel());
 		values.put("devolveCauseLabel", value);
 		
		value = new ColumnValue(obj.getWorkingHourUsed());
 		values.put("workingHourUsed", value);
		value = new ColumnValue(obj.getMachineHourUsed());
 		values.put("machineHourUsed", value);
 		
		value = new ColumnValue(obj.getDeductAmount());
 		values.put("deductAmount", value);
		value = new ColumnValue(obj.getApportionAmount());
 		values.put("apportionAmount", value);
 		
		value = new ColumnValue(obj.getBrand());
 		values.put("brand", value);
		value = new ColumnValue(obj.getBrandLabel());
 		values.put("brandLabel", value);
		value = new ColumnValue(obj.getPlaceOrigin());
 		values.put("placeOrigin", value);
		value = new ColumnValue(obj.getManufacturer());
 		values.put("manufacturer", value);
		
		value = new ColumnValue(obj.getBatchNo());
 		values.put("batchNo", value);
		value = new ColumnValue(obj.getBatchDescribe());
 		values.put("batchDescribe", value);
		value = new ColumnValue(obj.getProduceDate());
 		values.put("produceDate", value);
		value = new ColumnValue(obj.getValidDate());
 		values.put("validDate", value);
		value = new ColumnValue(obj.getCheckoutAgainDate());
 		values.put("checkoutAgainDate", value);
 		
		value = new ColumnValue(obj.getStoreNo());
 		values.put("storeNo", value);
		value = new ColumnValue(obj.getAbcGrade());
 		values.put("abcGrade", value);
		
		value = new ColumnValue(obj.getItemSize());
 		values.put("itemSize", value);
		value = new ColumnValue(obj.getOrderWeight());
 		values.put("itemWeight", value);
 		
		value = new ColumnValue(obj.getOrderSize());
 		values.put("orderSize", value);
		value = new ColumnValue(obj.getOrderWeight());
 		values.put("orderWeight", value);

		value = new ColumnValue(obj.getWorkNo());
 		values.put("workNo", value);
		value = new ColumnValue(obj.getProcedureStep());
 		values.put("procedureStep", value);
		value = new ColumnValue(obj.getProjectNo());
 		values.put("projectNo", value);
		value = new ColumnValue(obj.getOrderNo());
 		values.put("orderNo", value);
		value = new ColumnValue(obj.getCheckoutTimes());
 		values.put("checkoutTimes", value);
		value = new ColumnValue(obj.getBarCode());
 		values.put("barCode", value);

//		value = new ColumnValue(obj.getDepartmentNo());
// 		values.put("departmentNo", value);
//		value = new ColumnValue(obj.getDepartmentName());
// 		values.put("departmentName", value);
		value = new ColumnValue(obj.getEmployeeNo());
 		values.put("employeeNo", value);
		value = new ColumnValue(obj.getEmployeeName());
 		values.put("employeeName", value);
		
		value = new ColumnValue(obj.getComments());
 		values.put("comments", value);

		value = new ColumnValue(obj.getUserId());
 		values.put("userId", value);
		value = new ColumnValue(obj.getEnterpriseNo());
 		values.put("enterpriseNo", value);
		value = new ColumnValue(obj.getEnterpriseName());
 		values.put("enterpriseName", value);

		value = new ColumnValue(obj.getVersion());
 		values.put("version", value);
		
		Row row = new Row(values,RowState.NONE);
		return row;
	}
	
	public StorageItemBook rowToObject(Row row){
		StorageItemBook obj = new StorageItemBook();
		obj.setfID(row.getString("fID"));
	    
		obj.setSystemCode(row.getString("systemCode"));			
		obj.setBillType(row.getString("billType"));
		obj.setBillTypeName(row.getString("billTypeName"));
		obj.setOrderDate(row.getDate("orderDate"));
		obj.setState(row.getString("state"));
		obj.setStatus(row.getString("status"));

		obj.setRevOpay(row.getString("revOpay"));
		obj.setDirection(row.getInteger("direction"));
		obj.setCostingCode(row.getString("costingCode"));
		obj.setAudit(row.getString("audit"));
		obj.setConfirmDate(row.getDate("confirmDate"));
		
		obj.setOrigin(row.getString("origin"));
		obj.setOriginName(row.getString("originName"));
		obj.setOriginNo(row.getString("originNo"));
		
		obj.setRecordNo(row.getString("recordNo"));
		
		obj.setFactoryNo(row.getString("factoryNo"));
		obj.setFactoryName(row.getString("factoryName"));

		obj.setStorageTypeD(row.getString("storageTypeD"));
		obj.setStorageTypeDLabel(row.getString("storageTypeDLabel"));
		obj.setProviderNo(row.getString("providerNo"));
		obj.setShortName(row.getString("shortName"));
		obj.setProviderName(row.getString("providerName"));
		
		obj.setStorageType(row.getString("storageType"));
		obj.setStorageTypeLabel(row.getString("storageTypeLabel"));
		obj.setStorageNo(row.getString("storageNo"));
		obj.setStorageName(row.getString("storageName"));
		obj.setFloorNo(row.getString("floorNo"));

		obj.setTaxType(row.getInteger("taxType"));
		obj.setTaxTypeLabel(row.getString("taxTypeLabel"));
		obj.setTaxRate(row.getDecimal("taxRate"));
		
		obj.setfMasterID(row.getString("fMasterID"));
		
		obj.setItemNo(row.getString("itemNo"));
		obj.setItemDescribe(row.getString("itemDescribe"));
		obj.setItemProperty(row.getString("itemProperty"));
		obj.setUnitCode(row.getString("unitCode"));
		obj.setUnitName(row.getString("unitName"));
		obj.setProcess(row.getString("process"));
		obj.setProcessLabel(row.getString("processLabel"));
		
		obj.setMaterialType(row.getInteger("materialType"));
		obj.setMaterialTypeLabel(row.getString("materialTypeLabel"));
		
		obj.setQualityStatus(row.getInteger("qualityStatus"));
		obj.setQualityStatusLabel(row.getString("qualityStatusLabel"));

		obj.setItemPrice(row.getDecimal("itemPrice"));
		obj.setDiscountRate(row.getDecimal("discountRate"));
		
		obj.setArriveAmount(row.getDecimal("arriveAmount"));
		obj.setWasterAmount(row.getDecimal("wasterAmount"));
		obj.setUllageAmount(row.getDecimal("ullageAmount"));
		obj.setArriveAmountBack(row.getDecimal("arriveAmountBack"));
		obj.setPriceAmount(row.getDecimal("priceAmount"));
		
		obj.setItemAmount(row.getDecimal("itemAmount"));
		obj.setOrderAmount(row.getDecimal("orderAmount"));
		obj.setOrderAmountOld(row.getDecimal("orderAmountOld"));
		obj.setOrderAmountTax(row.getDecimal("orderAmountTax"));
		obj.setOrderAmountTotal(row.getDecimal("orderAmountTotal"));
		
		obj.setAccountQuantityI(row.getDecimal("accountQuantityI"));
		obj.setAccountQuantityO(row.getDecimal("accountQuantityO"));
		
		obj.setPieceQuantity(row.getDecimal("pieceQuantity"));
		obj.setPieceQuantityI(row.getDecimal("pieceQuantityI"));
		obj.setPieceQuantityO(row.getDecimal("pieceQuantityO"));
		
		obj.setQuantityL(row.getDecimal("quantityL"));
		obj.setAmountL(row.getDecimal("amountL"));
		obj.setCostingL(row.getDecimal("costingL"));
		obj.setPieceQuantityL(row.getDecimal("pieceQuantityL"));
		
		obj.setWasterItemIng(row.getDecimal("wasterItemIng"));
		obj.setWasterItemAmount(row.getDecimal("wasterItemAmount"));
		obj.setWasterPiece(row.getDecimal("wasterPiece"));
		
		obj.setInvoiceQuantityIng(row.getDecimal("invoiceQuantityIng"));
		obj.setInvoiceQuantity(row.getDecimal("invoiceQuantity"));
		obj.setInvoiceAmount(row.getDecimal("invoiceAmount"));

		obj.setEvaluateQuantityIng(row.getDecimal("evaluateQuantityIng"));
		obj.setEvaluateQuantity(row.getDecimal("evaluateQuantity"));
		obj.setEvaluateAmount(row.getDecimal("evaluateAmount"));
		
		obj.setBackItemIng(row.getDecimal("backItemIng"));
		obj.setBackItemAmount(row.getDecimal("backItemAmount"));
		obj.setBackPiece(row.getDecimal("backPiece"));
		
		obj.setMaterialAmount(row.getDecimal("materialAmount"));
		obj.setHumanAmount(row.getDecimal("workingAmount"));
		obj.setManufactAmount(row.getDecimal("manufactAmount"));
		obj.setProcessAmount(row.getDecimal("processAmount"));
		obj.setCostingAmount(row.getDecimal("costingAmount"));
		
		obj.setMaterialAmountPer(row.getDecimal("materialAmountPer"));
		obj.setHumanAmountPer(row.getDecimal("workingAmountPer"));
		obj.setManufactAmountPer(row.getDecimal("manufactAmountPer"));
		obj.setProcessAmountPer(row.getDecimal("processAmountPer"));
		obj.setCostingAmountPer(row.getDecimal("costingAmountPer"));
		
		obj.setDevolveCause(row.getString("devolveCause"));
		obj.setDevolveCauseLabel(row.getString("devolveCauseLabel"));

		obj.setWorkingHourUsed(row.getDecimal("workingHourUsed"));
		obj.setMachineHourUsed(row.getDecimal("machineHourUsed"));

		obj.setDeductAmount(row.getDecimal("deductAmount"));//扣款金额
		obj.setApportionAmount(row.getDecimal("apportionAmount"));//进货费用(分摊)
		
		obj.setBatchNo(row.getString("batchNo"));
		obj.setBatchDescribe(row.getString("batchDescribe"));
		obj.setProduceDate(row.getDate("produceDate"));
		obj.setValidDate(row.getDate("validDate"));
		obj.setCheckoutAgainDate(row.getDate("checkoutAgainDate"));
		
		obj.setBrand(row.getString("brand"));
		obj.setBrandLabel(row.getString("brandLabel"));
		obj.setPlaceOrigin(row.getString("placeOrigin"));
		obj.setManufacturer(row.getString("manufacturer"));

		obj.setStoreNo(row.getString("storeNo"));
		obj.setAbcGrade(row.getString("abcGrade"));
		
		obj.setItemWeight(row.getDecimal("itemWeight"));
		obj.setItemSize(row.getString("itemSize"));

		obj.setOrderWeight(row.getDecimal("orderWeight"));
		obj.setOrderSize(row.getString("orderSize"));
		
		obj.setWorkNo(row.getString("workNo"));
		obj.setProcedureStep(row.getString("procedureStep"));
		obj.setProjectNo(row.getString("projectNo"));
		obj.setOrderNo(row.getString("orderNo"));
		
		obj.setCheckoutTimes(row.getString("checkoutTimes"));
		obj.setBarCode(row.getString("barCode"));
		
		obj.setEmployeeNo(row.getString("employeeNo"));
		obj.setEmployeeName(row.getString("employeeName"));
//		obj.setDepartmentNo(row.getString("departmentNo"));
//		obj.setDepartmentName(row.getString("departmentName"));

		obj.setComments(row.getString("comments"));
		
		obj.setUserId(row.getString("userId"));

		obj.setEnterpriseNo(row.getString("enterpriseNo"));
		obj.setEnterpriseName(row.getString("enterpriseName"));
		
		obj.setVersion(row.getInteger("version"));
		
		obj.setRowState(row.getState());

		return obj;
	}
*/	
	
	
	///////////////////////业务功能///////////////////////////////////////
	

	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStorageItemBookService#getOrderLLD(int, java.sql.Date, java.sql.Date, java.lang.String)
	 */
	@Override
	public List<StorageLedgerDetail> getCostingLLDList(String workNo, Date dateStart, Date dateEnd, String enterpriseNo) {
		return dao.getCostingLLDList(workNo, dateStart, dateEnd, enterpriseNo);
	}
	
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStorageItemBookService#getOrderRKD(int, java.sql.Date, java.sql.Date, java.lang.String)
	 */
	@Override
	public List<StorageLedgerDetail> getCostingRKDList(Date dateStart, Date dateEnd, String enterpriseNo) {
		return dao.getCostingRKDList(dateStart, dateEnd, enterpriseNo);
	}
	
	//成本核算相关的委外单列表
	@Override
	public List<StorageLedgerDetail> getCostingWWDList(Date dateStart, Date dateEnd, String enterpriseNo) {
		return dao.getCostingWWDList(dateStart, dateEnd, enterpriseNo);
	}
	
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IStorageItemBookService#getOrderRKDByProduceNo(int, java.lang.String, java.sql.Date, java.sql.Date, java.lang.String)
	 */
	@Override
	public List<StorageLedgerDetail> getCostingRKDListByWorkNo(Date dateStart, Date dateEnd, String workNo, String enterpriseNo) {
		return dao.getCostingRKDListByWorkNo(dateStart, dateEnd, workNo, enterpriseNo);
	}
	
	@Transactional
	@Override
	public StorageLedgerDetail insert(
			String recordNo 
			,String serialNo
			,String fMasterID
			
			,String systemCode 
			,String billType 
			,String billTypeName 
			,String revOpay 
			,String costingCode 
			,int dealType

			,Date orderDate 
			
			,String tradeobjType
			,String tradeobjTypeLabel
			,String tradeobjID
			,String tradeobjNo
			,String shortName
			,String tradeobjName
			
			,String storageType
			,String storageTypeLabel
			,String storageID
			,String storageNo
			,String storageName
			,String floorNo
			//仓库ID
			//,String storehouseID

			,String itemID
			,String itemNo
			,String itemName
			,String itemProperty
			
			,String unitCode
			,String unitName
			
			,int validDays
			,int checkoutAgainDays
			
			,BigDecimal itemQuantity
			,BigDecimal pieceQuantity 

			,BigDecimal materialAmountPer 
			,BigDecimal workingAmountPer 
			,BigDecimal manufactAmountPer 
			,BigDecimal processAmountPer 
			,BigDecimal costingAmountPer 

			,BigDecimal materialAmount 
			,BigDecimal workingAmount 
			,BigDecimal manufactAmount 
			,BigDecimal processAmount 
			,BigDecimal costingAmount 
			
			,BigDecimal itemQuantityL
			,BigDecimal pieceQuantityL
			,BigDecimal costingAmountL
			,BigDecimal costingAmountPerL

			,String batchNo
			,String batchDescribe
			,Date produceDate 
			,Date validDate 
			,Date checkoutAgainDate 

			,String storeNo
			
			,String projectNo
			,String workNo
			
			,int frozenFlag

			,String userId 
			,String enterpriseNo 
			,String enterpriseName 

			){
		
		//新增登记出入库记录
		String fID = Utils.getUUID();
		StorageLedgerDetail srd = new StorageLedgerDetail();
		srd.setfID(fID);
		
		srd.setRecordNo(recordNo);
		srd.setSerialNo(serialNo);
		srd.setfMasterID(fMasterID);

		srd.setSystemCode(systemCode);
		srd.setBillType(billType);
		srd.setBillTypeName(billTypeName);
		srd.setRevOpay(revOpay);
		srd.setCostingCode(costingCode);
		srd.setDealType(dealType);
		
		srd.setOrderDate(orderDate);
		
		srd.setTradeobjType(tradeobjType);
		srd.setTradeobjTypeLabel(tradeobjTypeLabel);
		srd.setTradeobjID(tradeobjID);
		srd.setTradeobjNo(tradeobjNo);
		srd.setShortName(shortName);
		srd.setTradeobjName(tradeobjName);

		srd.setStorageType(storageType);
		srd.setStorageTypeLabel(storageTypeLabel);
		srd.setStorageID(storageID);
		srd.setStorageNo(storageNo);
		srd.setStorageName(storageName);
		srd.setFloorNo(floorNo);
		//srd.setStorehouseID(storehouseID);
		
		srd.setItemID(itemID);
		srd.setItemNo(itemNo);
		srd.setItemName(itemName);
		srd.setItemProperty(itemProperty);
		
		srd.setUnitCode(unitCode);
		srd.setUnitName(unitName);
		
		srd.setValidDate(validDate);
		srd.setCheckoutAgainDays(checkoutAgainDays);
		
		srd.setItemQuantity(itemQuantity);
		srd.setPieceQuantity(pieceQuantity);
		
		srd.setMaterialAmount(materialAmount);
		srd.setWorkingAmount(workingAmount);
		srd.setManufactAmount(manufactAmount);
		srd.setProcessAmount(processAmount);
		srd.setCostingAmount(costingAmount);
		
		srd.setMaterialAmountPer(materialAmountPer);
		srd.setWorkingAmountPer(workingAmountPer);
		srd.setManufactAmountPer(manufactAmountPer);
		srd.setProcessAmountPer(processAmountPer);
		srd.setCostingAmountPer(costingAmountPer);
		
		srd.setItemQuantityL(itemQuantityL);
		srd.setPieceQuantityL(pieceQuantityL);
		srd.setCostingAmountL(costingAmountL);
		srd.setCostingAmountPerL(costingAmountPerL);
		
		
		srd.setBatchNo(batchNo);
		srd.setBatchDescribe(batchDescribe);
		srd.setProduceDate(produceDate);
		srd.setValidDate(validDate);
		srd.setCheckoutAgainDate(checkoutAgainDate);
		
		srd.setStoreNo(storeNo);
		
		srd.setProjectNo(projectNo);
		srd.setWorkNo(workNo);
		
		srd.setFrozenFlag(frozenFlag);
		
		srd.setUserId(userId);
		srd.setEnterpriseNo(enterpriseNo);
		srd.setEnterpriseName(enterpriseName);
		srd.setfMasterID(fMasterID);
		
		srd.setVersion(0);
		
		dao.insert(srd);
		
		return srd;
	}
	
	
	
	
	@Transactional
	@Override
	public StorageLedgerDetail insert(
			String recordNo 
			,String serialNo
			,String fMasterID
			
			,String systemCode 
			,String billType 
			,String billTypeName 
			,String revOpay 
			,String costingCode 
			,int dealType

			,Date orderDate 
			
			,String tradeobjType
			,String tradeobjTypeLabel
			,String tradeobjID
			,String tradeobjNo
			,String shortName
			,String tradeobjName
			
			,String storageType
			,String storageTypeLabel
			,String storageID
			,String storageNo
			,String storageName
			,String floorNo
			//仓库ID
			//,String storehouseID

			,String itemID
			,String itemNo
			,String itemName
			,String itemProperty
			
			,String unitCode
			,String unitName
			
			,int validDays
			,int checkoutAgainDays
			
			,BigDecimal itemQuantity
			,BigDecimal pieceQuantity 

			,BigDecimal materialAmountPer 
			,BigDecimal workingAmountPer 
			,BigDecimal manufactAmountPer 
			,BigDecimal processAmountPer 
			,BigDecimal costingAmountPer 

			,BigDecimal materialAmount 
			,BigDecimal workingAmount 
			,BigDecimal manufactAmount 
			,BigDecimal processAmount 
			,BigDecimal costingAmount 

			,String batchNo
			,String batchDescribe
			,Date produceDate 
			,Date validDate 
			,Date checkoutAgainDate 

			,String storeNo
			
			,String projectNo
			,String workNo
			
			,int frozenFlag

			,String userId 
			,String enterpriseNo 
			,String enterpriseName 

			){
		
		//新增登记出入库记录
		String fID = Utils.getUUID();
		StorageLedgerDetail srd = new StorageLedgerDetail();
		srd.setfID(fID);
		
		srd.setRecordNo(recordNo);
		srd.setSerialNo(serialNo);
		srd.setfMasterID(fMasterID);

		srd.setSystemCode(systemCode);
		srd.setBillType(billType);
		srd.setBillTypeName(billTypeName);
		srd.setRevOpay(revOpay);
		srd.setCostingCode(costingCode);
		srd.setDealType(dealType);
		
		srd.setOrderDate(orderDate);
		
		srd.setTradeobjType(tradeobjType);
		srd.setTradeobjTypeLabel(tradeobjTypeLabel);
		srd.setTradeobjID(tradeobjID);
		srd.setTradeobjNo(tradeobjNo);
		srd.setShortName(shortName);
		srd.setTradeobjName(tradeobjName);

		srd.setStorageType(storageType);
		srd.setStorageTypeLabel(storageTypeLabel);
		srd.setStorageID(storageID);
		srd.setStorageNo(storageNo);
		srd.setStorageName(storageName);
		srd.setFloorNo(floorNo);
		//srd.setStorehouseID(storehouseID);
		
		srd.setItemID(itemID);
		srd.setItemNo(itemNo);
		srd.setItemName(itemName);
		srd.setItemProperty(itemProperty);
		
		srd.setUnitCode(unitCode);
		srd.setUnitName(unitName);
		
		srd.setValidDate(validDate);
		srd.setCheckoutAgainDays(checkoutAgainDays);
		
		srd.setItemQuantity(itemQuantity);
		srd.setPieceQuantity(pieceQuantity);
		
		srd.setMaterialAmount(materialAmount);
		srd.setWorkingAmount(workingAmount);
		srd.setManufactAmount(manufactAmount);
		srd.setProcessAmount(processAmount);
		srd.setCostingAmount(costingAmount);
		
		srd.setMaterialAmountPer(materialAmountPer);
		srd.setWorkingAmountPer(workingAmountPer);
		srd.setManufactAmountPer(manufactAmountPer);
		srd.setProcessAmountPer(processAmountPer);
		srd.setCostingAmountPer(costingAmountPer);
		
//		srd.setItemQuantityL(itemQuantityL);
//		srd.setPieceQuantityL(pieceQuantityL);
//		srd.setCostingAmountL(costingAmountL);
//		srd.setCostingAmountPerL(costingAmountPerL);
		
		
		srd.setBatchNo(batchNo);
		srd.setBatchDescribe(batchDescribe);
		srd.setProduceDate(produceDate);
		srd.setValidDate(validDate);
		srd.setCheckoutAgainDate(checkoutAgainDate);
		
		srd.setStoreNo(storeNo);
		
		srd.setProjectNo(projectNo);
		srd.setWorkNo(workNo);
		
		srd.setFrozenFlag(frozenFlag);
		
		srd.setUserId(userId);
		srd.setEnterpriseNo(enterpriseNo);
		srd.setEnterpriseName(enterpriseName);
		srd.setfMasterID(fMasterID);
		
		srd.setVersion(0);
		
		dao.insert(srd);
		
		return srd;
	}
	
	
	
	
	////////////////////做为主表时使用//////////////////////////////////////
	
	@Override
	public JSONObject newData(JSONObject data) {
		return dao.create(data);
	}
	
	@Override
	public JSONObject deleteData(JSONObject param) {
//		String ucode = param.getString("ucode");
//		String uname = param.getString("uname");
//		String uid = param.getString("uid");
		
		JSONArray ids = param.getJSONArray("ids");
		for(int i=0;i<ids.size();i++){
			String id = ids.getString(i);
			//这里可以增加是否可删除的判断

			//然后删除主表
			dao.delete(id);
		}

		JSONObject data = new JSONObject();
		data.put("ids", ids);
		
		JSONObject json = new JSONObject();
		json.put("code", 0);
		json.put("msg", "success");
		json.put("data", data);
		
		return json;
	}

	@Override
	public JSONObject saveData(JSONObject param) {
		// TODO Auto-generated method stub
		JSONObject data;
		try {
			//保存主表
			data = this.save(param);
					
			JSONObject json = new JSONObject();
			json.put("code", 0);
			json.put("msg", "success");
			json.put("data", data);
			
			return json;
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			log.info("开始打印ParseException");
			e.printStackTrace();
			log.info("e.getMessage()-->>"+e.getMessage());
			log.info("完成打印ParseException");
			
			JSONObject json = new JSONObject();
			json.put("code", -1);
			json.put("msg", e.getMessage());
			
			return json;
		} 
	}

	@Override
	public JSONObject queryData(JSONObject param) {
		// TODO Auto-generated method stub
		try {
			JSONObject jsonTable = dao.query(param);
			
			JSONObject json = new JSONObject();
			json.put("code", 0);
			json.put("msg", "success");
			json.put("data", jsonTable);
			
			return json;
		} catch (SQLException e) {
			e.printStackTrace();
			
			JSONObject json = new JSONObject();
			json.put("code", -1);
			json.put("msg", e.getMessage());
			return json;
		}
	}
	@Override
	public JSONObject validData(JSONObject param) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<StorageLedgerDetail> getListByItemID(String itemID, Date dateStart, Date dateEnd,	String enterpriseNo) {
		return dao.getListByItemID(itemID, dateStart, dateEnd, enterpriseNo);
	}

	
	
	
	
	
}
